﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Steamrunner
{
    class SpriteSheet : DrawableGameComponent
    {
        #region Variables
        SpriteBatch spriteBatch = null;
        string filename;

        Texture2D texture = null;
        Vector2 origin = Vector2.Zero;
        Point frameSize = Point.Zero;
        Point currentFrame = Point.Zero;
        Point sheetSize = Point.Zero;
        int timeSinceLastFrame = 0;
        int millisecondsPerFrame = 0;

        Vector2 position = Vector2.Zero;
        Color color = Color.White;
        float rotation = 0f;
        float scale = 1.0f;
        float depth = 0.0f;
        #endregion

        #region Properties

        public Point CurrentFrame
        {
            get { return currentFrame; }
            set { currentFrame = value; }
        }
        
        public SpriteBatch SpriteBatch
        {
            set { spriteBatch = value; }
        }

        public Vector2 Origin
        {
            get { return origin; }
        }
        
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        public Color TintColor
        {
            get { return color; }
            set { color = value; }
        }

        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        public float Depth
        {
            get { return depth; }
            set { depth = value; }
        }
        #endregion

        public SpriteSheet(Game game, string filename, Vector2 origin, Point frameSize, Point currentFrame, Point sheetSize, int millisecondsPerFrame, SpriteBatch spriteBatch)
            : base(game)
        {
            this.filename = filename;
            this.origin = origin;
            this.frameSize = frameSize;
            this.currentFrame = currentFrame;
            this.sheetSize = sheetSize;
            this.millisecondsPerFrame = millisecondsPerFrame;
            this.spriteBatch = spriteBatch;
        }

        protected override void LoadContent()
        {
            texture = Game.Content.Load<Texture2D>(filename);
            
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame >= millisecondsPerFrame)
            {
                timeSinceLastFrame = 0;
                currentFrame.X++;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.X = 0;
                    currentFrame.Y++;

                    if (currentFrame.Y >= sheetSize.Y)
                    {
                        currentFrame.Y = 0;
                    }
                }
            }
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Rectangle textureRect = new Rectangle(
                currentFrame.X * frameSize.X,
                currentFrame.Y * frameSize.Y,
                frameSize.X, frameSize.Y);

            spriteBatch.Draw(texture, position, textureRect, color, rotation, origin, 1.0f, SpriteEffects.None, depth);
            
            base.Draw(gameTime);
        }
    }
}
